//资料检查
import { View, Button, Text, Image, Form } from "@tarojs/components";
import "./index.scss";
import React, { useState, useEffect } from "react";
import Taro, { getStorageSync } from "@tarojs/taro";
import store from "../../store";
import { observer } from "mobx-react";
import http from "../../utils/http";
let Photo = () => {
  let [textData, setTextData] = useState([
    { id: 1, key: "所属拼团点" },
    { id: 2, key: "门店类型" },
    { id: 3, key: "门店名称" },
    { id: 4, key: "社会统一信用代码" },
    { id: 5, key: "团长" },
    { id: 6, key: "身份证号" },
    { id: 7, key: "门店地址" },
    { id: 8, key: "邀请码" }
  ]);
  let [imgData, setImgData] = useState([]);
  const [userImg, setUserImg] = useState("");
  useEffect(() => {
    setImgData([...store.s_wh.imgMeans]);
    let data = textData.map((v, i) => {
      v.value = store.s_wh.textMeans[i].value;
      return v;
    });
    data.push({ id: 9, key: "联系电话", value: store.s_wh.user.phone });
    setTextData(data);
    if (Taro.getStorageSync("userImg")) {
      setUserImg(Taro.getStorageSync("userImg"));
    }
  }, []);
  //
  let submit = function(e) {
    Taro.showModal({
      title: "提示",
      content: "请仔细检查并确认资料无误",
      cancelText: "再检查下",
      confirmText: "确认提交",
      confirmColor: "#F23A3A",
      success: function(res) {
        if (res.confirm) {
          http({
            url: "/merchant/updateStoreOneController",
            method: "post",
            data: {
              ...store.s_wh.means,
              ...store.s_wh.user
            }
          }).then(res => {
            console.log(res);
            imgData.forEach(v => {
              Taro.uploadFile({
                url: "http://localhost:/admin/store/user/license", //仅为示例，非真实的接口地址
                filePath: v,
                name: "files",
                formData: {
                  ...store.s_wh.user
                },
                success: res => {
                  console.log("图片", res);
                }
              });
            });
            Taro.navigateTo({
              url: "./under"
            });
          });
        }
      }
    });
  };
  let reset = function(e) {
    Taro.navigateTo({
      url: "./photo"
    });
  };
  let TextMean = () =>
    textData.map(v => (
      <View key={v.id}>
        <Text>{v.key}：</Text>
        <Text>{v.value}</Text>
      </View>
    ));

  let ImgMean = () => imgData.map(v => <Image src={v}></Image>);
  return (
    <View className="means">
      <View className="tit">
        <View>检查资料</View>
        <View>
          <Image src={userImg}></Image>
        </View>
      </View>
      <View className="text">
        <TextMean></TextMean>
      </View>
      <View className="img">
        <ImgMean></ImgMean>
      </View>
      <Form onSubmit={submit} onReset={reset}>
        <View className="but">
          <Button form-type="reset">上一步</Button>
          <Button form-type="submit">提交审核</Button>
        </View>
      </Form>
    </View>
  );
};
export default observer(Photo);
